home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagd_f.zip / FILES.SWG / 0051_INI File Handler.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-18  |  6KB  |  242 lines

  1.  
  2. {**********************************************************************
  3.  ** UNIT SETUP2                                                      **
  4.  ** Handles an *.INI-File similiar to Windows                        **
  5.  **********************************************************************
  6.  ** The Setup-variables all have a unique name an can be retrieved   **
  7.  ** by using the name. A default-value must be given when retrieving **
  8.  ** a value, so that the returned value is always valid!             **
  9.  ** There are different functions for different data types           **
  10.  ** A BAK-file is created when touching the INI-file                 **
  11.  ** For added speed a copy of the INI file is held in variable SETUP **
  12.  **********************************************************************
  13.  ** This is untested stuff, it runs flawlessly here in my programs   **
  14.  ** (c) 1994 by Dirk Paessler, given to the public domain            **
  15.  ** if you change anything please note; leave my name in here!!      **
  16.  ** if you have questions or suggestions, please contact me          **
  17. ┌───────────────────┬─────────────┬───────────────────────────────────┐
  18. │ Dirk Paessler     │             │ E-Mail:       FIDO 2:2490/1145.15 │
  19. │ Laerchenweg 8     │Fax          │ CIS 100114,42      2:2490/2091.5  │
  20. │ D-91058 Erlangen  │+499131601169│ internet 100114.42@compuserve.com │
  21. └───────────────────┴─────────────┴───────────────────────────────────┘
  22.  
  23.  usage:
  24.  
  25.  USES setup2;
  26.  VAR MyData:string;
  27.  BEGIN
  28.    Mydata:=GetStrProfile('MyData','nothing yet');
  29.    WriteLn(mydata);
  30.    PutStrProfile('MyData','New stuff');
  31.    Mydata:=GetStrProfile('MyData','nothing yet');
  32.    WriteLn(mydata);
  33.  END.
  34.  
  35.  
  36.  }
  37.  
  38. UNIT Setup2;
  39. INTERFACE
  40.   
  41. FUNCTION GetIntProfile(name:STRING; default:INTEGER):INTEGER;
  42. PROCEDURE PutRealProfile(name:STRING; wert:REAL);
  43. FUNCTION GetRealProfile(name:STRING; default:REAL):REAL;
  44. PROCEDURE PutStrProfile(name,wert:STRING);
  45. PROCEDURE PutBoolProfile(name:STRING; wert:BOOLEAN);
  46. FUNCTION GetStrProfile(name,default:STRING):STRING;
  47. FUNCTION GetNumProfile(name:STRING):REAL;
  48. FUNCTION GetBoolProfile(name:STRING; default:BOOLEAN):BOOLEAN;
  49.   
  50. TYPE PSetup = ^Setuptype;
  51. SetupType = ARRAY [1 .. 70] OF STRING[140];
  52.   
  53. VAR   Setup          : PSetup;
  54. IMPLEMENTATION
  55.   
  56. VAR     q:INTEGER;
  57. CONST anzsetups:INTEGER=0;
  58.   newsetup:BOOLEAN=TRUE;
  59.   
  60.   
  61. FUNCTION ReadALine(VAR Fil:TEXT):STRING;
  62.   VAR a:CHAR; b:STRING;
  63. BEGIN
  64.   b:='';
  65.   a:=#13;
  66.   
  67.   WHILE (a<>#10) AND NOT (EOF(FIL)) DO
  68.   BEGIN
  69.     IF a<>#13 THEN b:=b+a;
  70.     Read(fil,a);
  71.   END;
  72.   ReadAline:=b;
  73. END;
  74.   
  75. PROCEDURE Zerleg(a:STRING; VAR b,c:STRING);
  76.   VAR i:INTEGER;
  77. BEGIN
  78.   i:=0;
  79.   REPEAT
  80.     i:=i+1;
  81.   UNTIL (a[i]='=') OR (i>length(a));
  82.  
  83.   IF i>length(a) THEN i:=length(a);
  84.   b:=copy(a,1,i-1);
  85.   c:=copy(a,i+1,length(a)-i);
  86. END;
  87.  
  88. FUNCTION FileExist(Fname:string):BOOLEAN;
  89. VAR f:file;
  90. BEGIN
  91. {$I-}
  92.   Assign(f,fname);
  93.   Reset(f);
  94.   Close(f);
  95. {$I+}
  96.   FileExist := (IOResult=0) and (fname<>'');
  97. END;
  98.  
  99.  
  100. PROCEDURE ReadSetup;
  101.   VAR MyFil:TEXT;a,myname,wert:STRING;
  102. BEGIN
  103.   IF NOT Fileexist('astro5.ini') THEN
  104.   BEGIN
  105.     Assign(MyFil,'astro5.ini');
  106.     Rewrite(MyFil);
  107.     WriteLn(MyFil,';  ***                    PSCS-Astro V5 INI                       ***');
  108.     Close(MyFil);
  109.   END;
  110.   IF Setup=NIL THEN
  111.   BEGIN
  112.     New(setup);
  113.   END;
  114.   Assign(MyFil,'astro5.ini');
  115.   Reset(MyFil);
  116.   q:=1;
  117.   REPEAT
  118.     REPEAT
  119.       a:=ReadALine(MyFil);
  120.     UNTIL (a[1]<>';') OR (eof(myfil));
  121.     setup^[q]:=a;
  122.     q:=q+1;
  123.   UNTIL (EOF(MyFil));
  124.   anzsetups:=q-1;
  125.   Close(MyFil);
  126.   NewSetup:=FALSE;
  127. END;
  128.   
  129.   
  130. FUNCTION GetStrProfile(name,default:STRING):STRING;
  131.   VAR MyFil:TEXT;a,myname,wert:STRING;
  132. BEGIN
  133.   GetStrProfile:=default;
  134.   
  135.   IF Fileexist('astro5.ini') THEN
  136.   BEGIN
  137.     IF Setup=NIL THEN
  138.     BEGIN
  139.       New(setup);
  140.     END;
  141.     IF NewSetup THEN ReadSetup;
  142.     q:=1;
  143.     REPEAT
  144.       Zerleg(setup^[q],MyName,wert);
  145.       q:=q+1;
  146.     UNTIL (name=MyName) OR (q>anzsetups);
  147.     IF name=MyName THEN GetStrProfile:=wert;
  148.   END;
  149. END;
  150.   
  151. FUNCTION GetBoolProfile(name:STRING; default:BOOLEAN):BOOLEAN;
  152.   VAR hlpstrg:STRING;
  153. BEGIN
  154.   hlpstrg:=GetStrProfile(name,'t');
  155.   GetBoolProfile := default;
  156.   IF hlpstrg='TRUE' THEN GetBoolProfile := TRUE;
  157.   IF hlpstrg='FALSE' THEN GetBoolProfile := FALSE;
  158. END;
  159.   
  160. PROCEDURE PutBoolProfile(name:STRING; wert:BOOLEAN);
  161.   VAR hlpstrg:STRING;
  162. BEGIN
  163.   hlpstrg:='FALSE';
  164.   IF wert THEN hlpstrg:='TRUE';
  165.   PutStrProfile(name,hlpstrg);
  166. END;
  167.   
  168. FUNCTION GetIntProfile(name:STRING; default:INTEGER):INTEGER;
  169. BEGIN
  170.   GetIntProfile:=Round(GetRealProfile(name,default*1.0));
  171. END;
  172.   
  173. FUNCTION GetRealProfile(name:STRING; default:REAL):REAL;
  174.   VAR hlpstrg:STRING; i:INTEGER; a:REAL;
  175. BEGIN
  176.   str(default,hlpstrg);
  177.   hlpstrg:=GetStrProfile(name,hlpstrg);
  178.   val(hlpstrg,a,i);
  179.   GetRealProfile:=a;
  180. END;
  181.   
  182. PROCEDURE PutRealProfile(name:STRING; wert:REAL);
  183.   VAR hlpstrg:STRING;
  184. BEGIN
  185.   Str(wert:1:10,hlpstrg);
  186.   PutStrProfile(name,hlpstrg);
  187. END;
  188.   
  189.   
  190. PROCEDURE PutStrProfile(name,wert:STRING);
  191.   VAR MyFil,my2fil,my3fil:TEXT;a,myname,altwert,Mywert:STRING; WasIt:BOOLEAN;
  192. BEGIN
  193.   altwert:=getStrProfile(name,'#*äöü');
  194.   IF altwert=wert THEN exit;
  195.   
  196.   IF NOT Fileexist('astro5.ini') THEN
  197.   BEGIN
  198.     Assign(MyFil,'astro5.ini');
  199.     Rewrite(MyFil);
  200.     WriteLn(MyFil,';  ***                    PSCS-Astro V5 INI                       ***');
  201.     Close(MyFil);
  202.   END;
  203.   Assign(MyFil,'astro5ini.tmp');
  204.   Rewrite(MyFil);
  205.   Assign(My2Fil,'astro5.ini');
  206.   ReSet(My2Fil);
  207.   WasIt:=FALSE;
  208.   REPEAT
  209.     a:=ReadALine(My2fil);
  210.     Zerleg(a,myname,mywert);
  211.     IF myname=name THEN BEGIN
  212.       WriteLn(myfil,name,'=',wert);
  213.       WasIt:=TRUE;
  214.     END
  215.     ELSE WriteLn(myfil,a)
  216.   UNTIL EOF(my2fil);
  217.   IF NOT WasIt THEN WriteLn(myfil,name,'=',wert);
  218.   Close(MyFil);
  219.   Close(My2Fil);
  220.   IF Fileexist('astro5.bak') THEN
  221.   BEGIN
  222.     Assign (my3fil,'astro5.bak');
  223.     erase(my3fil);
  224.   END;
  225.   Rename(My2Fil,'astro5.bak');
  226.   Rename(MyFil,'astro5.ini');
  227.   ReadSetup;
  228. END;
  229.   
  230. FUNCTION GetNumProfile(name:STRING):REAL;
  231. BEGIN
  232. END;
  233.   
  234. BEGIN
  235.   
  236.   
  237. END.
  238.   
  239. {be sure to insert the following line into your exit-code!!!}
  240.  
  241. IF setup<>NIL THEN Dispose(setup);
  242.